package com.atguigu.gmall.list.receiver;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.list.dao.GoodsRepositoryDao;
import com.atguigu.gmall.list.service.SearchService;
import com.rabbitmq.client.Channel;
import net.bytebuddy.asm.Advice;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 接收消息
 * 添加索引
 * 删除索引
 */
@Component
public class ListReceiver {

    @Autowired
    private SearchService searchService;

    //接收消息 上架
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_GOODS_UPPER,durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS,durable = "true",autoDelete = "false"),
            key = {MqConst.ROUTING_GOODS_UPPER}
    ))
    public void upperMessage(String skuId, Message message, Channel channel) throws Exception {
        //添加索引
        searchService.upperGoods(Long.parseLong(skuId));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
    //接收消息 下架
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_GOODS_LOWER,durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS,durable = "true",autoDelete = "false"),
            key = {MqConst.ROUTING_GOODS_LOWER}
    ))
    public void lowerMessage(String skuId, Message message, Channel channel) throws Exception {
        //删除索引
        searchService.lowerGoods(Long.parseLong(skuId));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
